package com.cote.douhahou.pretloan.home;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import com.cote.douhahou.pretloan.beans.ProcessInAkomapalendfo;
import com.cote.douhahou.pretloan.network.HttpHelper;
import com.cote.douhahou.pretloan.status.BaseAkomapalendLnFragment;
import com.cote.douhahou.pretloan.status.LnAKomapalendRpyFragment;
import com.cote.douhahou.pretloan.status.LnStatAkomapalendeFailFragment;
import com.cote.douhahou.pretloan.status.LoanStaAkomapalendteWaitFragment;
import com.cote.douhahou.pretloan.utils.AkomapaLendConfigs;
import com.cote.douhahou.pretloan.utils.AkomapaLendUtil;
import com.cote.douhahou.pretloan.R;

import pub.devrel.easypermissions.EasyPermissions;

public class HomeActivity extends AppCompatActivity {

    private TextView dsTextView, loanTextView, pcTextView;
    private View dsView, loanView, pcView;
    private View dsClickView, loanClickView, pcClickView;

    private BaseAkomapalendLnFragment homeFragment;
    private PersonalCenterFragment pcFragment;
    private AgreementFragment dsFragment;

    private int flag = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);

        dsTextView = findViewById(R.id.ama_tv_ds);
        loanTextView = findViewById(R.id.ama_tv_ln);
        pcTextView = findViewById(R.id.ama_tv_pc);

        dsView = findViewById(R.id.ama_view_ds);
        loanView = findViewById(R.id.ama_view_ln);
        pcView = findViewById(R.id.ama_view_pc);

        dsClickView = findViewById(R.id.ama_view_ds_click);
        loanClickView = findViewById(R.id.ama_view_ln_click);
        pcClickView = findViewById(R.id.ama_view_pc_click);

        init();
    }

    private void init(){

        pcFragment = new PersonalCenterFragment();
        dsFragment = new AgreementFragment();

        dsClickView.setOnClickListener(v -> toDsPage());
        loanClickView.setOnClickListener(v -> toHomePage());
        pcClickView.setOnClickListener(v -> toPcPage());

        //用户状态
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction
                .add(R.id.am_layout_fragment, dsFragment)
                .add(R.id.am_layout_fragment, pcFragment)
                .commitAllowingStateLoss();
    }

    @Override
    protected void onResume() {
        super.onResume();
        HttpHelper.inst().submitEvent("MAIN","START","");
        updateState();
    }

    private void updateState(){
        String state = ProcessInAkomapalendfo.instance().getAkomapaLendUtil();
        if (AkomapaLendUtil.stateIsChange(state)) updateState2(state);
    }

    private void updateState2(String userState) {

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

        if (homeFragment != null) fragmentTransaction.remove(homeFragment);

        switch (userState){
            case AkomapaLendConfigs.StateREpay:
                homeFragment = new LnAKomapalendRpyFragment();
                break;
            case AkomapaLendConfigs.StateWait:
                homeFragment = new LoanStaAkomapalendteWaitFragment();
                break;
            case AkomapaLendConfigs.StateFail:
                homeFragment = new LnStatAkomapalendeFailFragment();
                break;
            default:
                homeFragment = new HomeFragment();
        }

        fragmentTransaction.add(R.id.am_layout_fragment, homeFragment);

        switch (flag) {
            case 1:
                fragmentTransaction.show(dsFragment).hide(homeFragment).hide(pcFragment);
                break;
            case 2:
                fragmentTransaction.show(pcFragment).hide(homeFragment).hide(dsFragment);
                break;
            default:
                fragmentTransaction.show(homeFragment).hide(dsFragment).hide(pcFragment);
        }

        fragmentTransaction.commitAllowingStateLoss();
    }

    private void toDsPage(){

        HttpHelper.inst().submitEvent("MAIN","DATA_SAFETY_CLICK","");

        flag = 1;

        dsTextView.setTextColor(getColor(R.color.tab_text_black));
        loanTextView.setTextColor(getColor(R.color.tab_text_gray));
        pcTextView.setTextColor(getColor(R.color.tab_text_gray));

        dsView.setVisibility(View.VISIBLE);
        loanView.setVisibility(View.INVISIBLE);
        pcView.setVisibility(View.INVISIBLE);

        getSupportFragmentManager().beginTransaction()
                .show(dsFragment)
                .hide(homeFragment)
                .hide(pcFragment)
                .commitAllowingStateLoss();

    }

    private void toHomePage(){

        HttpHelper.inst().submitEvent("MAIN","HOME_CLICK","");

        flag = 0;

        dsTextView.setTextColor(getColor(R.color.tab_text_gray));
        loanTextView.setTextColor(getColor(R.color.tab_text_black));
        pcTextView.setTextColor(getColor(R.color.tab_text_gray));
        dsView.setVisibility(View.INVISIBLE);
        loanView.setVisibility(View.VISIBLE);
        pcView.setVisibility(View.INVISIBLE);

        getSupportFragmentManager().beginTransaction()
                .show(homeFragment)
                .hide(dsFragment)
                .hide(pcFragment)
                .commitAllowingStateLoss();

    }

    private void toPcPage(){

        HttpHelper.inst().submitEvent("MAIN","ME_CLICK","");

        flag = 2;

        dsTextView.setTextColor(getColor(R.color.tab_text_gray));
        loanTextView.setTextColor(getColor(R.color.tab_text_gray));
        pcTextView.setTextColor(getColor(R.color.tab_text_black));
        dsView.setVisibility(View.INVISIBLE);
        loanView.setVisibility(View.INVISIBLE);
        pcView.setVisibility(View.VISIBLE);

        getSupportFragmentManager().beginTransaction()
                .show(pcFragment)
                .hide(dsFragment)
                .hide(homeFragment)
                .commitAllowingStateLoss();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AkomapaLendUtil.clearState();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (homeFragment !=null && homeFragment instanceof HomeFragment) EasyPermissions.onRequestPermissionsResult(requestCode,permissions,grantResults, homeFragment);
    }

    @Override
    protected void onPause() {
        super.onPause();
        HttpHelper.inst().submitEvent("MAIN","END","");
    }

}